package com.ding.pojo;


import lombok.*;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name="users")
@Data
public class User{
	@Id
	private String username;
	private String password;
	private int enabled=1;
	//mappedBy指定用来描述关系的Authority中的属性
	@OneToMany(mappedBy="user",fetch= FetchType.LAZY,cascade= {CascadeType.PERSIST, CascadeType.REMOVE})
	private List<Authority> authorities=new ArrayList<>();

	public User(String username, String password) {
		this.username=username;
		this.password=password;
	}

	public void addAuthority(String authority) {
		Authority auth = new Authority(this, authority);
		authorities.add(auth);
	}


	public void removeAuthority(String authority) {
		for (Authority auth : authorities) {
			if (auth.getAuthority().equals(authority)) {
				authorities.remove(auth);
				return;
			}
		}
	}

}
